/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package me.crosswall.lib.coverflow;

import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * 适配器
 *
 * @since 2021-03-26
 */
public class PageSliderAdapter extends PageSliderProvider {
    private List<Integer> pages;
    private Context context;

    /**
     * 构造方法
     *
     * @param pages q
     * @param context q
     */
    public PageSliderAdapter(ArrayList<Integer> pages, Context context) {
        this.pages = pages;
        this.context = context;
    }

    @Override
    public int getCount() {
        return pages == null ? 0 : pages.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i1) {
        Component component = null;
        component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_pager_item, null, false);
        if (!(component instanceof ComponentContainer)) {
            return "";
        }
        componentContainer.addComponent(component);
        componentContainer.setClipEnabled(false);
        component.setClipEnabled(false);
        Image image = (Image) componentContainer.findComponentById(ResourceTable.Id_image_pager);
        component.setTag(this);
        image.setPixelMap(pages.get(i1));
        image.setClipEnabled(false);
        image.setScaleMode(Image.ScaleMode.STRETCH);
        return componentContainer;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i1, Object object) {
        if (componentContainer == null) {
            return;
        }
        if (object instanceof Component) {
            componentContainer.removeComponent((Component) object);
        }
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object ob) {
        return component == ob;
    }

}
